#########################################################################
################## Section 5
################## Maturity
################## Plot results 
#########################################################################
# we plot only long-term credit, i.e. loans and credit lines, on debtor level

# import libraries
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

path_data = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final datasets'
path_charts = r'P:\ECB business areas\DGR\Staff\papouts\Research\Active\Firm Heterogeneities\Final charts\Maturity\final'
if not os.path.isdir(path_charts):
    os.makedirs(path_charts)

# define plotting function                  
def plot_weighted_mtrty(data, color, order):    
    # data should be a list of dataframes
    # color should be a text
    # order should be a list of lists, where the first is order of all countries, the second is selected countries and the third is the order of type of loans
                        
    # single plots by size for selected countries
    sns.set(rc={'figure.figsize':(15,10), 'axes.labelsize':26, 'axes.titlesize':26,  'axes.labelpad':15, 'xtick.labelsize':26, 'ytick.labelsize':26}, style='white')
    for i, j in enumerate(['Large', 'Medium', 'Small', 'Micro']):
        plt.figure()
        sns.boxplot(data=data[0].loc[data[0]['CNTRY'].isin(order[1])][data[0].loc[data[0]['CNTRY'].isin(order[1])].sz_f == (i+1)], x='CNTRY', y='ONA_W_MTRTY_BYDBTR_y', width=[0.6], whis=[25, 75], showfliers=False, showmeans=True, medianprops={'color':'red','linewidth':4}, meanprops={'markerfacecolor':'red', 'markeredgecolor':'red', 'markersize':12}, color=color, order=order[1]).set(xlabel='', ylabel='Maturity (years)')
        plt.savefig(path_charts + '\\Section5_mtrty_bysz_' + j + '_201912.pdf', dpi=600, bbox_inches='tight')

    
# load data
AC_bytype = pd.read_stata(path_data + r'\instr_lvl_dt_f.dta')
AC_bytype.drop(columns=['prtctn_amnt_byinstr', 'ona_intrate_c_ttl','ona_pddbtr_c_ttl', 'wir_byloantype', 'pddbtr_byloantype'], inplace=True)
AC_bytype.drop(AC_bytype[AC_bytype.nace_code == ''].index, inplace=True)


# get debtor level data set
# instrument level
AC_final_bytype = AC_bytype[AC_bytype.mtrty_byloantype.isna() == False].copy()
# debtor level (weighted)
# debtor dataset should include only credit lines and loans
ona_dbtr = AC_final_bytype[AC_final_bytype.typ_instr_corr.isin(['Loans','Credit lines'])].groupby(['cntry_dbtr','dbtr_id'])['ona_orgnlmtrty_c_ttl'].sum().reset_index().rename(columns={'ona_orgnlmtrty_c_ttl':'ona_dbtr_longterm'})
AC_final_bytype['onaxmtr'] = AC_final_bytype['ona_orgnlmtrty_c_ttl']*AC_final_bytype['mtrty_byloantype']
mtr_dbtr = AC_final_bytype[AC_final_bytype.typ_instr_corr.isin(['Loans','Credit lines'])].groupby(['cntry_dbtr','dbtr_id'])['onaxmtr'].sum().reset_index().rename(columns={'onaxmtr':'weighted_sum'})
merge_dbtr = ona_dbtr.merge(mtr_dbtr, how='inner', on=['cntry_dbtr','dbtr_id'])
merge_dbtr['mtrty_dbtr'] = merge_dbtr['weighted_sum']/merge_dbtr['ona_dbtr_longterm']
AC_final_bydbtr = AC_final_bytype[['cntry_dbtr','dbtr_id','sz_f','nace_code']].drop_duplicates().merge(merge_dbtr[['cntry_dbtr','dbtr_id','mtrty_dbtr']], how='inner',on=['cntry_dbtr','dbtr_id'])

# rename columns to match function
AC_final_bydbtr.rename(columns={'cntry_dbtr':'CNTRY','mtrty_dbtr':'ONA_W_MTRTY_BYDBTR_y'}, inplace=True)

# order
#order_cntry = ['AT','BE','CY','DE','EE','ES','FI','FR','GR','IE','IT','LT','LU','LV','MT','NL','PT','SI','SK']
sel_countries = ['AT','BE','DE','ES','FI','FR','GR','IE','IT','NL','PT']
order_cntry_dbtr = AC_final_bydbtr[AC_final_bydbtr.sz_f == 1].groupby(['CNTRY'])['ONA_W_MTRTY_BYDBTR_y'].mean().sort_values(ascending=False).index
order_slctd_cntry_dbtr = order_cntry_dbtr[order_cntry_dbtr.isin(sel_countries)]

# plot
plot_weighted_mtrty(data=[AC_final_bydbtr], color='#7ccaa5', order=[order_cntry_dbtr, order_slctd_cntry_dbtr])

